package com.gf.message.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gf.base.type.MessageState;
import com.gf.base.type.MessageType;
import com.gf.common.bean.Page;
import com.gf.common.staticer.MessageRemainsHelper;
import com.gf.message.dao.MessageDao;
import com.gf.message.model.Message;
import com.gf.message.service.MessageService;

@Transactional
@Service("messageService")
public class MessageServiceImpl implements MessageService {

	@Autowired
	private MessageDao messageDao;

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public long sendMessageByOne(long senderId, String senderName,
			long receiveId, String title, String content) {
		long result = messageDao.insert(senderId, senderName, receiveId, "",
				"", title, content, MessageType.OUTBOX, MessageState.READED,
				MessageState.UNREAD);
		if (result > 0) {
			MessageRemainsHelper.updateRemainsByOne(receiveId);
		}
		return result;
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public int sendMessageByGroup(long senderId, String senderName,
			Long[] receiveIds, String title, String content) {
		int n = messageDao.batchInsert(senderId, senderName, receiveIds, "",
				"", title, content, MessageType.OUTBOX, MessageState.READED,
				MessageState.UNREAD);
		if (n == receiveIds.length) {
			MessageRemainsHelper.updateRemainsByOne(receiveIds);
		}
		return n;
	}

	@Override
	public Page getAllSendMessages(long senderId, int curpage, int pageSize) {
		Page page = new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);

		List<Message> messages = messageDao.queryMessagesByMessageType(
				senderId, MessageType.OUTBOX, page);
		page.setTotal(messageDao.queryNumberByMessageType(senderId,
				MessageType.OUTBOX));
		page.setResults(messages);

		return page;
	}

	@Override
	public Page getAllDraftMessages(long senderId, int curpage, int pageSize) {
		Page page = new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);
		int count = messageDao.queryNumberByMessageType(senderId,
				MessageType.DRAFT);
		List<Message> messages = messageDao.queryMessagesByMessageType(
				senderId, MessageType.DRAFT, page);
		page.setResults(messages);
		page.setTotal(count);
		return page;
	}

	@Override
	public Page getAllReceiveMessages(long receiveId, int curpage, int pageSize) {
		Page page = new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);
		int count = messageDao.queryNumberOfReceiveMessages(receiveId);
		List<Message> messages = messageDao.queryReceiveMessages(receiveId,
				page);
		page.setResults(messages);
		page.setTotal(count);
		return page;
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void deleteMessageBySender(long senderId) {
		messageDao.updateSenderState(senderId, MessageState.DELETED);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void deleteMessageByReceiver(long receiverId) {
		messageDao.updateReceiverState(receiverId, MessageState.DELETED);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void messageReaded(long id) {
		messageDao.updateReceiverState(id, MessageState.READED);
	}

	@Override
	public int getNumberOfUnreadMessages(long receiveId) {

		return messageDao.queryNumberOfUnreadMessages(receiveId);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public long saveAsDraft(long senderId, String senderName,
			String draftReceiveIds, String draftReceiveNames, String title,
			String content) {

		return messageDao.insert(senderId, senderName, senderId,
				draftReceiveIds, draftReceiveNames, title, content,
				MessageType.DRAFT, MessageState.READED, MessageState.UNREAD);
	}

	@Override
	public int getNumberOfReceiveMessages(long receiveId) {
		return messageDao.queryNumberOfReceiveMessages(receiveId);
	}

	@Override
	public int getNumberByMessageType(long senderId, MessageType messageType) {
		return messageDao.queryNumberByMessageType(senderId, messageType);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void deleteDraft(long id) {
		messageDao.updateMessageType(id, MessageType.RECYCLE);

	}

}
